package com.dongdongshop.log;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
//声明切面类
@Aspect
public class LogAspect {
    private final Logger logger= LoggerFactory.getLogger(LogAspect.class);

    //定义切点表达式
    @Pointcut("execution(public * com.dongdongshop.service..*.*(..))") //要切到那个方法下面 ..*.*(..)切到所有包或者子包..*下的所有类.*下的所有方法..
    public void aopLog(){}

    //前置通知
    @Before("aopLog()")
    public void doBefore(JoinPoint joinPoint)throws  Throwable{
        //获取所有参数
        Object[] args = joinPoint.getArgs();
        //获取方法 =要切的是那个方法
        Signature signature = joinPoint.getSignature();
        logger.info("正在执行的方法为: {} ,参数是: {}",signature,args);
    }

    //返回后通知                           //returning 返回的值
    @AfterReturning(pointcut = "aopLog()",returning = "result")
                       //Object result是返回的结果
    public void doAfter(Object result)throws Throwable{
        logger.info("方法执行成功，返回结果为:{}",JSONObject.toJSONString(result));
    }
}
